package com.atguigu.srb.sms.controller;

import com.atguigu.common.exception.BusinessAssert;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.srb.sms.client.CoreUserInfoClient;
import com.atguigu.srb.sms.service.SmsService;
import com.atguigu.srb.sms.util.RandomUtils;
import com.atguigu.srb.sms.util.RegexValidateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

/**
 * @program: srb
 * @author: 龙龙
 * @create: 2022-02-09 14:14
 * @description:
 **/
@RestController
@RequestMapping("/api/sms/")
@Api(value = "短信接口")
@CrossOrigin
public class ApiSmsController {

    @Autowired
    private SmsService smsService;

    @Autowired
    private CoreUserInfoClient userInfoClient;




    @ApiOperation("获取短信验证码")
    @GetMapping("/send/{mobile}")
    public R send(
            @ApiParam("手机号码")
            @PathVariable("mobile") String mobile){

        //校验手机号
        boolean b = RegexValidateUtils.checkCellphone(mobile);
        BusinessAssert.isTrue(b, ResponseEnum.MOBILE_ERROR);

        //校验手机号是否被注册
//        int i = 1 / 0 ;
        boolean b1 = userInfoClient.checkMobile(mobile);
        System.out.println(b1);
        BusinessAssert.isTrue(!b1,ResponseEnum.MOBILE_EXIST_ERROR);

        //调用业务层 存入redis 发送手机号和验证码到阿里云
        smsService.send(mobile);

        return R.ok();
    }




}





